#!/usr/bin/python2.7
#coding=utf8
from ana_excel import Config
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from ana_excel.common.modules import Base

class DataBase(object):
    def __init__(self):
        config = Config()
        self.SQLALCHEMYCONN = config.SQLALCHEMYCONN
        self.SQLALCHEMYECHO = config.SQLALCHEMYECHO

    def dbsession(self):
        echo = True if self.SQLALCHEMYECHO == "True" else False
        engine = create_engine(self.SQLALCHEMYCONN, echo=echo, pool_recycle=5)
        Session = sessionmaker(bind=engine)
        self.session = Session()
        return self.session

    def close(self):
        self.session.close()

def init_db():
    database = DataBase()
    engine = create_engine(database.SQLALCHEMYCONN, echo=True)
    Base.metadata.create_all(engine)